package com.googlecode.continuity.sample.basic.client;

import java.util.*;

import com.google.gwt.user.client.ui.*;
import com.googlecode.continuity.core.client.ConfigurationEntry;

/**
 * ConfigPanel
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class ConfigPanel extends Composite {
// ------------------------------ FIELDS ------------------------------

  private FlexTable list;
  private Map entries;
  private Button submit;

// --------------------------- CONSTRUCTORS ---------------------------

  public ConfigPanel() {
    list = new FlexTable();
    entries = new HashMap();
    submit = new Button("submit");

    // force left align of configuration entries
    HorizontalPanel leftAlign = new HorizontalPanel();
    HTML spacer = new HTML("&nbsp;");
    spacer.setWidth("100%");
    leftAlign.add(list);
    leftAlign.add(spacer);

    // add control buttons for reset and submit
    HorizontalPanel buttons = new HorizontalPanel();
    final ConfigPanel self = this;
    buttons.add(new Button("reset", new ClickListener() {
      public void onClick(Widget sender) {
        self.resetRows();
      }
    }));
    buttons.add(submit);
    list.setWidget(0, 0, buttons);
    list.getFlexCellFormatter().setColSpan(0, 0, 3);
    list.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);

    // initialize
    initWidget(leftAlign);
  }

  public void resetRows() {
    updateRows();
  }

  public void updateRows() {
    Iterator it = entries.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry entry = (Map.Entry) it.next();
      updateRow(((Integer) entry.getKey()).intValue(), (ConfigurationEntry) entry.getValue());
    }
  }

  public void updateRow(int index, ConfigurationEntry entry) {
    // TODO: use message!
    list.setText(index, 0, entry.getDescriptionCode() + ":");
    ((TextBox) list.getWidget(index, 1)).setText(entry.getValue());
/*

    if (configEntry.getErrors() == null) {
      return;
    }

    switch (configEntry.getErrors().length) {
      case 0:
        list.removeCell(index, 2);
        break;
      case 1:
        list.setText(index, 2, configEntry.getErrors()[0]);
        break;
      default:
        HorizontalPanel errors = new HorizontalPanel();
        for (int i = 0; i < configEntry.getErrors().length; i++) {
          errors.add(new Label(configEntry.getErrors()[i]));
        }
        list.setWidget(index, 2, errors);
    }
*/
  }

  public ConfigPanel(List entries) {
    this();
    addEntries(entries);
  }

  public void addEntries(List entries) {
    Iterator it = entries.iterator();
    while (it.hasNext()) {
      addEntry((ConfigurationEntry) it.next());
    }
  }

  public void addEntry(ConfigurationEntry entry) {
    int targetRow = list.getRowCount() - 1;
    list.insertRow(targetRow);
    list.setWidget(targetRow, 1, new TextBox());
    entries.put(new Integer(targetRow), entry);
    updateRow(targetRow, entry);
  }

// -------------------------- OTHER METHODS --------------------------

  public void addSubmitListener(ClickListener listener) {
    submit.addClickListener(listener);
  }

  public List readRows() {
    List ret = new ArrayList();
    for (int i = 0; i < list.getRowCount() - 1; i++) {
      ret.add(readRow(i));
    }
    return ret;
  }

  public ConfigurationEntry readRow(int index) {
    ConfigurationEntry entry = (ConfigurationEntry) entries.get(new Integer(index));
    entry.setValue(((TextBox) list.getWidget(index, 1)).getText());
    return entry;
  }
}
